import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '@/views/Home.vue'
// import Welcome from '@/components/Welcome.vue'
// import Users from '@/components/Users/Users.vue'
// import Rights from '@/components/Power/Rights.vue'
// import Roles from '@/components/Power/Roles.vue'
// import Goods from '@/components/goods/goods.vue'
// import Params from '@/components/goods/params.vue'
// import Categories from '@/components/goods/categories.vue'
// import Orders from '@/components/orders/orders.vue'
// import Reports from '@/components/reports/reports.vue'
// import GoodsAdd from '@/components/goods/goodsAdd.vue'
// import Login from '@/views/Login.vue'
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '@/views/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/views/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Welcome.vue')
const Users = () => import(/* webpackChunkName: "user" */ '@/components/Users/Users.vue')
const Rights = () => import(/* webpackChunkName: "rights_roles" */ '@/components/Power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "rights_roles" */ '@/components/Power/Roles.vue')
const Goods = () => import(/* webpackChunkName: "goods" */ '@/components/goods/goods.vue')
const Params = () => import(/* webpackChunkName: "goods" */ '@/components/goods/params.vue')
const Categories = () => import(/* webpackChunkName: "goods" */ '@/components/goods/categories.vue')
const GoodsAdd = () => import(/* webpackChunkName: "goods" */ '@/components/goods/goodsAdd.vue')
const Orders = () => import(/* webpackChunkName: "orders_reports" */ '@/components/orders/orders.vue')
const Reports = () => import(/* webpackChunkName: "orders_reports" */ '@/components/reports/reports.vue')
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login, meta: { title: '登录' } },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome, meta: { title: '欢迎来到后台管理系统' } },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/goods', component: Goods },
      { path: '/params', component: Params },
      { path: '/categories', component: Categories },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports },
      { path: '/goods/add', component: GoodsAdd }
    ]
  }
]

const router = new VueRouter({
  routes
})
// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') next()
//   const tokenStr = window.sessionStorage.getItem('token')
//   if (!tokenStr) return next('/login')
//   next()
// })
// 全局后置守卫：初始化时、每次路由切换后执行
router.afterEach((to, from) => {
  if (to.meta.title) {
    document.title = to.meta.title // 修改网页的title
  } else {
    document.title = '随心购后台管理系统'
  }
})

router.beforeEach((to, from, next) => {
  const token = window.sessionStorage.getItem('token')
  if (token && to.path !== '/login') {
    // 有token 但不是去 login页面 通过
    next()
  } else if (token && to.path === '/login') {
    // 有token 但是去 login页面 不通过 重定向到首页
    next('/')
  } else if (!token && to.path !== '/login') {
    // 没有token 但不是去 login页面 不通过（未登录不给进入）
    next('/login')
  } else {
    // 剩下最后一种 没有token 但是去 login页面 通过
    next()
  }
})

export default router
